package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Sysregion;
import com.ruoyi.system.service.ISysregionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * sysregionController
 * 
 * @author ruoyi
 * @date 2021-10-28
 */
@Controller
@RequestMapping("/system/sysregion")
public class SysregionController extends BaseController
{
    private String prefix = "system/sysregion";

    @Autowired
    private ISysregionService sysregionService;

    @RequiresPermissions("system:sysregion:view")
    @GetMapping()
    public String sysregion()
    {
        return prefix + "/sysregion";
    }

    /**
     * 查询sysregion列表
     */
    @RequiresPermissions("system:sysregion:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Sysregion sysregion)
    {
        startPage();
        List<Sysregion> list = sysregionService.selectSysregionList(sysregion);
        return getDataTable(list);
    }

    /**
     * 导出sysregion列表
     */
    @RequiresPermissions("system:sysregion:export")
    @Log(title = "sysregion", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Sysregion sysregion)
    {
        List<Sysregion> list = sysregionService.selectSysregionList(sysregion);
        ExcelUtil<Sysregion> util = new ExcelUtil<Sysregion>(Sysregion.class);
        return util.exportExcel(list, "sysregion数据");
    }

    /**
     * 新增sysregion
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存sysregion
     */
    @RequiresPermissions("system:sysregion:add")
    @Log(title = "sysregion", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Sysregion sysregion)
    {
        return toAjax(sysregionService.insertSysregion(sysregion));
    }

    /**
     * 修改sysregion
     */
    @GetMapping("/edit/{KeyId}")
    public String edit(@PathVariable("KeyId") Long KeyId, ModelMap mmap)
    {
        Sysregion sysregion = sysregionService.selectSysregionById(KeyId);
        mmap.put("sysregion", sysregion);
        return prefix + "/edit";
    }

    /**
     * 修改保存sysregion
     */
    @RequiresPermissions("system:sysregion:edit")
    @Log(title = "sysregion", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Sysregion sysregion)
    {
        return toAjax(sysregionService.updateSysregion(sysregion));
    }

    /**
     * 删除sysregion
     */
    @RequiresPermissions("system:sysregion:remove")
    @Log(title = "sysregion", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysregionService.deleteSysregionByIds(ids));
    }

    @PostMapping("/selectbyshangjiid")
    @ResponseBody
    public List<Map<String,Object>> selectByShangjiId(String shangjiid){
        List<Map<String,Object>> mmList  = sysregionService.selectByShangjiId(shangjiid);
        return mmList;
    }

    @PostMapping("/regionlist")
    @ResponseBody
    public TableDataInfo regionlist()
    {
        startPage();
        List<Map<String,Object>> mmList = new ArrayList<>();
        List<Map<String,Object>> mmRegionList = sysregionService.selectByShangjiId("100000");

        for (int i=0;i<mmRegionList.size();i++){

            Map<String,Object> mmMap = new HashMap<>();
            mmMap.put("Code",mmRegionList.get(i).get("KeyId").toString());
            mmMap.put("Name",mmRegionList.get(i).get("RegionName").toString());
            mmMap.put("level",geterji(mmRegionList.get(i).get("KeyId").toString()));

            mmList.add(mmMap);
        }

        return getDataTable(mmList);
    }


    public  List<Map<String,Object>>  geterji(String mmId){
        List<Map<String,Object>> mmRegionList = sysregionService.selectByShangjiId(mmId);
        List<Map<String,Object>> mmList = new ArrayList<>();
        for (int i=0;i<mmRegionList.size();i++){
            Map<String,Object> mmMap = new HashMap<>();

            mmMap.put("Code",mmRegionList.get(i).get("KeyId").toString());
            mmMap.put("Name",mmRegionList.get(i).get("RegionName").toString());
            mmMap.put("sort",i+1);
            mmMap.put("level",getsanji(mmRegionList.get(i).get("KeyId").toString()));

            mmList.add(mmMap);
        }
        return mmList;
    }
    public  List<Map<String,Object>>  getsanji(String mmId){
        List<Map<String,Object>> mmRegionList = sysregionService.selectByShangjiId(mmId);
        List<Map<String,Object>> mmList = new ArrayList<>();
        for (int i=0;i<mmRegionList.size();i++){
            Map<String,Object> mmMap = new HashMap<>();

            mmMap.put("Code",mmRegionList.get(i).get("KeyId").toString());
            mmMap.put("Name",mmRegionList.get(i).get("RegionName").toString());
            mmMap.put("sort",i+1);

            mmList.add(mmMap);
        }
        return mmList;
    }
}
